<?php
	require'../init.php';
	$id = $_GET['id'];
	$sql = "select a.*,b.picName from stu a inner join photo b on a.pid = b.id where a.id = $id";
	$result = mysql_query($sql);
	$row = mysql_fetch_assoc($result);
	var_dump($row);
	
	//表单提交
	if($_POST){
		$sql2 = "update stu set 
						name = '$_POST[name]',
						birthday = '$_POST[birthday]',
						qq = '$_POST[qq]',
						phone = '$_POST[phone]',
						homepage = '$_POST[homepage]',
						info = '$_POST[info]' where id = $id
		";
		$res2 = @mysql_query($sql2);
	}
	
	//文件上传
	if(!empty($_FILES)){
		$pic_info = $_FILES['pic'];
		if($pic_info['error'] > 0){
			switch ($pic_info['error']){
				case 1 : $error_msg.= '文件大小超过了 php.ini中 upload_max_filesize选项限制的值！';
					break ;
				case 2 : $error_msg.= '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
					break ;
				case 3 : $error_msg.= '文件只有部分被上传';
					break ;
				case 4 : $error_msg.= '没有文件被上传';
					break ;
				case 6 : $error_msg.= '找不到临时文件夹';
					break ;
				case 7 : $error_msg.= '文件写入失败';
					break ;
				default : $error_msg.= '未知错误!';
					break ;
			}
			echo $error_msg;
			return false;
		}
		//获取文件上传的类型
		$type = substr(strrchr($pic_info['name'],'.'),1);
		//判断上传文件的类型
		if($type !== 'jpg'){
			echo '你上传的不是图片';
			return false;
		}
		//图片重命名
		$new_file = $row['stu_num'].'.'.$type;
		//设置上传的路径
		$filename = '../static/img/'.$new_file;
		//图片上传到临时目录成功，将其保存到指定的目录中
		if(!move_uploaded_file($pic_info['tmp_name'],$filename)){
			echo '图片上传失败';
			return false;
		}
		//修改数据
		$sql1 = "update photo set picName = '$new_file' where id = $id";
		$res1 = @mysql_query($sql1);
	}
?>
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>编辑信息--《学生信息管理系统》</title>
    <link href="../static/lib/bootstrap/css/bootstrap.min.css" rel="stylesheet">
	  <link rel="stylesheet" type="text/css" href="../static/css/style.css"/>
	  <link rel="stylesheet" type="text/css" href="../static/lib/jquery.datetimepicker.css"/>
  </head>
  <body>
  	<!--头部-->
  	<div class="header">
	    <div class="jumbotron">
			  <h1>学生信息管理系统</h1>
			</div>
  	</div>
  	
  	<!--编辑-->
  	<div class="container">
  		<ol class="breadcrumb">
			  <li><a href="index.php">主页</a></li>
			  <li class="active">修改资料</li>
			</ol>
			<div class="row">
				<div class="col-md-6">
					<form class="form-horizontal" action="edit.php?id=<?php echo $row['id'];?>" method="post" enctype="multipart/form-data">
					  <div class="form-group">
					    <label for="inputText1" class="col-sm-2 control-label">学号</label>
					    <div class="col-sm-10">
					      <input disabled="disabled" type="text" class="form-control" id="inputText1" placeholder="学号" value="<?php echo $row['stu_num']?>" name="stu_num">
					    </div>
					  </div>
					  <div class="form-group">
					    <label for="inputText2" class="col-sm-2 control-label">姓名</label>
					    <div class="col-sm-10">
					      <input type="text" class="form-control" id="inputText2" placeholder="姓名" value="<?php echo $row['name']?>" name="name">
					    </div>
					  </div>
					  <div class="form-group">
					    <label class="col-sm-2 control-label"> 头像 </label>
					    <div class="col-sm-10">
					      <input type="file" name="pic">
					    </div>
					  </div>
					  <div class="form-group">
					    <label  class="col-sm-2 control-label">性别</label>
					    <div class="col-sm-10">
						    <label class="radio-inline">
							  <input type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1"> 男
							</label>
							<label class="radio-inline">
							  <input type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2"> 女
							</label>
					    </div>
					  </div>
					  <div class="form-group">
					    <label  class="col-sm-2 control-label">班级</label>
					    <div class="col-sm-10">
						    <select class="form-control" id="sel">
								  <option value="1">前端工程师</option>
								  <option value="2">UI设计师</option>
								  <option value="3">php工程师</option>
								  <option value="4">python工程师</option>
								  <option value="5">java工程师</option>
							</select>
					    </div>
					  </div>
					  <div class="form-group">
					    <label for="inputText3" class="col-sm-2 control-label"> 生日 </label>
					    <div class="col-sm-10">
					      <input type="text" class="form-control" id="birthday" placeholder="生日" value="<?php echo $row['birthday']?>" name="birthday">
					    </div>
					  </div>
					  <div class="form-group">
					    <label for="inputText4" class="col-sm-2 control-label"> Q Q </label>
					    <div class="col-sm-10">
					      <input type="text" class="form-control" id="inputText4" placeholder="QQ"  value="<?php echo $row['qq']?>" name="qq">
					    </div>
					  </div>
					  <div class="form-group">
					    <label for="inputText5" class="col-sm-2 control-label"> 手机 </label>
					    <div class="col-sm-10">
					      <input type="text" class="form-control" id="inputText5" placeholder="手机" value="<?php echo $row['phone']?>" name="phone">
					    </div>
					  </div>
					  <div class="form-group">
					    <label for="inputText6" class="col-sm-2 control-label"> 个人主页 </label>
					    <div class="col-sm-10">
					      <input type="text" class="form-control" id="inputText6" placeholder="个人主页" value="<?php echo $row['homepage']?>" name="homepage">
					    </div>
					  </div>
					  <div class="form-group">
					    <label for="inlineCheckbox" class="col-sm-2 control-label"> 个人爱好 </label>
					    <div class="col-sm-10">
					      	<label class="checkbox-inline">
							  <input type="checkbox" id="inlineCheckbox1" value="option1"> 阅读
							</label>
							<label class="checkbox-inline">
							  <input type="checkbox" id="inlineCheckbox2" value="option2"> 跑步
							</label>
							<label class="checkbox-inline">
							  <input type="checkbox" id="inlineCheckbox3" value="option3"> 爬山
							</label>
							<label class="checkbox-inline">
							  <input type="checkbox" id="inlineCheckbox3" value="option3"> 打篮球
							</label>
							<label class="checkbox-inline">
							  <input type="checkbox" id="inlineCheckbox2" value="option2"> 敲代码
							</label>
							<label class="checkbox-inline">
							  <input type="checkbox" id="inlineCheckbox3" value="option3"> 王者荣耀
							</label>
					    </div>
					  </div>
					 
					  <div class="form-group">
					    <label for="inputText7" class="col-sm-2 control-label"> 个人简介 </label>
					    <div class="col-sm-10">
					    	<textarea class="form-control" name="info" rows="4" cols="60"  id="inputText7"><?php echo $row['info']?></textarea>
					    </div>
					  </div>
					  <div class="form-group">
					    <div class="col-sm-offset-2 col-sm-10">
					      <button type="submit" class="btn btn-default">保存资料</button>
					      <button type="submit" class="btn btn-default">重新填写</button>
					    </div>
					  </div>
					</form>
				</div>
				<div class="col-md-6">
					<img src="<?php echo '../static/img/'.$row['picName']?>" class="pull-right img-circle" alt="" style="width:200px;height:200px;" />
				</div>
			</div>
  	</div>
	<!--底部-->
	<footer>
		<p>
			<a href="./index.html">首页</a>
			<a href="./photowall.html">照片墙</a>
			<a href="./add.html">添加学生</a>
		</p>
		<p>大涛课堂——《学生信息管理系统》</p>
	</footer>
	
	
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="../static/lib/bootstrap/js/bootstrap.min.js"></script>
    <script src="../static/lib/jquery.datetimepicker.js"></script>
    <script>
			//jQuery日历插件
			$(function(){
				var options = {lang:'ch',format:'Y-m-d',timepicker:false};
				$('#birthday').datetimepicker(options);
			});
     		document.getElementById("sel")[<?php echo $row[5]?>].selected = true;
		</script>
  </body>
</html>